/*
 * Auxiliar.cpp
 *
 *  Created on: 04/05/2012
 *      Author: Shazgath
 */

#include "common.h"
#include <iomanip>
#include "Util.h"
namespace Auxiliar {

int stoi(char* s) {
	std::stringstream ss(s);
	int res;
	ss >> res;
	return res;
}

int stoi(const std::string s) {
	std::stringstream ss(s.c_str());
	int res;
	ss >> res;
	return res;
}

std::string cambiarSeparadores(std::string& s, char sep1, char sep2) {
	std::string::iterator it;
	int size = s.size();
	for(int i = 0; i < size; ++i) {
		if(s[i] == sep1)
			s[i] = sep2;
	}
	return std::string(s);
}



std::string itos(int i) {
	std::string s;
	std::stringstream ss;
	ss << i;
	ss >> std::dec >> s;
	return s;
}


std::string int_to_hex( int i )
{
  std::stringstream stream;
  stream << std::setfill ('0') << std::setw(sizeof(int))
         << std::hex << i;
  return stream.str();
}

std::string int_to_dec( int i )
{
  std::stringstream stream;
  stream << std::setfill ('0') << std::setw(sizeof(int))
         << std::dec << i;
  return stream.str();
}

bool validarHecho(std::string s) {
	std::stringstream ss(s);
	unsigned int i;
	ss >> i;
	if(ss.fail())
		return false;
	else
		return true;
}

std::string float_to_dec(float f) {
	std::stringstream stream;
	stream << std::dec << f;
	return stream.str();
}
void parseAtrib(const std::string& atrib, std::string& nombreRes, int& pos) {
	std::cout << "parseando " << atrib << std::endl;
	int sep_pos = atrib.find(Util::NUMERAL, 0);
	std::cout << "posicion_separador " << sep_pos << std::endl;
	std::string pos_string = atrib.substr(0, sep_pos);
	std::string n = atrib.substr(sep_pos + 1, atrib.find(Util::PIPE) - sep_pos - 1);
	std::cout << "nombre " << n;

	pos = Auxiliar::stoi(pos_string);
	std::cout <<" pos " << pos << std::endl;
	nombreRes = n;
	std::cout << "fin parseo" << std::endl;
}


void parseLine(const std::string linea, std::vector<std::string>& tokens) {
	unsigned int pos = 0;
	while(linea.find(Util::PIPE, pos) != (linea.size() - 1)) {
		std::string token_s = linea.substr(pos, linea.find(Util::PIPE, pos) - pos);
		pos = linea.find(Util::PIPE, pos);
		if(!(pos == std::string::npos))
			pos++;
		tokens.push_back(token_s);
	}
	std::string token = linea.substr(pos, linea.find(Util::PIPE) - pos - 1);
	tokens.push_back(token);
}


}
